<?php 
/**
* 
*/
class Onload
{
	
	function __construct()
	{
		$this->ci =& get_instance();
	}

	public function checkSession(){

		$controller = $this->ci->router->class;
		$method = $this->ci->router->method;

		//echo $controller.' '.$method;
		//exit();
		
		if ($this->ci->session->userdata('userdata') == null) {

			if ($controller != "loginController") {
				redirect('loginController','refresh');
				exit();
			}
			
		}else{

			if( strtolower($controller) == 'logincontroller' && $method == 'index'){
				redirect('mainController','refresh');
				exit();
			}


			// PERMISSION PAGE
			$userdata = $this->ci->session->userdata('userdata');
			$permission = array();

			// Super Admin
			if ( strtolower($controller) == 'superadmincontroller') {
		    foreach ($userdata['UserPerm'] as $data) {
		      array_push($permission , trim($data['per_name']));
		    }
		    if (!in_array('per.superadmin', $permission)){
		    	redirect('mainController/','refresh');
		    }
			}

			// Admin
			// else if ( strtolower($controller) == 'employeecontroller') {

			// 	if ($method == 'changePassword') {
			// 		return;
			// 	}

			// 	foreach ($userdata['UserPerm'] as $data) {
		 //      array_push($permission , trim($data['per_name']));
		 //    }
		 //    if (!in_array('per.admin', $permission)){
		 //    	redirect('mainController/','refresh');
		 //    }
			// }
			else if ( strtolower($controller) == 'permissioncontroller') {
				foreach ($userdata['UserPerm'] as $data) {
		      array_push($permission , trim($data['per_name']));
		    }
		    if (!in_array('per.admin', $permission)){
		    	redirect('mainController/','refresh');
		    }
			}
			else if ( strtolower($controller) == 'referencecontroller') {
				foreach ($userdata['UserPerm'] as $data) {
		      array_push($permission , trim($data['per_name']));
		    }
		    if (!in_array('per.admin', $permission)){
		    	redirect('mainController/','refresh');
		    }
			}
			else if ( strtolower($controller) == 'rolecontroller') {
				foreach ($userdata['UserPerm'] as $data) {
		      array_push($permission , trim($data['per_name']));
		    }
		    if (!in_array('per.admin', $permission)){
		    	redirect('mainController/','refresh');
		    }
			}

		}

	}


}
?>